﻿using System.Collections.Generic;
using CakeLibrary;

namespace Medianamik.UI.Mediasend.Logic
{
    public class MediasendSession
    {
        internal string UserKey { get; set; }
        public string LastError { get; internal set; }

        internal MediasendSession(string userKey)
        {
            UserKey = userKey;
            LastError = string.Empty;
        }

        internal Dictionary<string, object> MakeBaseDictionary()
        {
            return new Dictionary<string, object>
                       {
                           {"user_key", UserKey}
                       };
        }

        public MediasendList GetList(int listId)
        {
            try
            {
                var dict = MakeBaseDictionary();
                dict.Add("list_id", listId.ToString());

                var list = MediasendList.MakeList(CakeList.GetInfo(dict));

                list.ContactCount = GetListContactCount(list);
                list.SubList = GetSubLists(list);

                return list;
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return null;
            }
        }

        public List<MediasendList> GetLists()
        {
            try
            {
                var dict = MakeBaseDictionary();
                var records = CakeList.GetList(dict);
                var lists = new List<MediasendList>();
                foreach (var kvp in records)
                {
                    var subLists = kvp.Value as Dictionary<string, object>;

                    foreach (var lst in subLists)
                    {
                        var list = MediasendList.MakeList(lst.Value as Dictionary<string, object>);
                        list.ContactCount = GetListContactCount(list);
                        list.SubList = GetSubLists(list);
                        lists.Add(list);
                    }
                }
                return lists;
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return null;
            }
        }

        public IEnumerable<MediasendSubList> GetSubLists(MediasendList MedList)
        {
            var request = MakeBaseDictionary();
            request.Add("list_id", MedList.ID);
            var records = CakeList.GetSublists(request);

            foreach (var kvp in records)
            {
                var subLists = kvp.Value as Dictionary<string, object>;

                foreach (var lst in subLists)
                {
                    var list = lst.Value as Dictionary<string, object>;
                    yield return MediasendSubList.MakeSublist(MedList, list);
                }
            }
        }

        public int GetListContactCount(MediasendList list)
        {
            var request = MakeBaseDictionary();
            request.Add("list_id", list.ID);
            var records = CakeList.GetInfo(request);

            if (records != null && records.ContainsKey("active"))
                return int.Parse(records["active"].ToString());

            return 0;
        }

        public IEnumerable<MediasendSubList> GetSubLists(int listId)
        {
            return GetSubLists(GetList(listId));
        }

        public MediasendCampaign GetCampaign(int campaignId)
        {
            try
            {
                var dict = MakeBaseDictionary();
                dict.Add("campaign_id", campaignId.ToString());
                return MediasendCampaign.MakeCampaign(CakeCampaign.GetInfo(dict));
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return null;
            }
        }

        public List<MediasendCampaign> GetCampaigns()
        {
            try
            {
                var dict = MakeBaseDictionary();
                var records = CakeCampaign.GetList(dict);
                var campaigns = new List<MediasendCampaign>();
                foreach (var kvp in records)
                {
                    var subCampaings = kvp.Value as Dictionary<string, object>;

                    foreach (var cpg in subCampaings)
                    {
                        campaigns.Add(MediasendCampaign.MakeCampaign(cpg.Value as Dictionary<string, object>));
                    }
                }
                return campaigns;
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return null;
            }
        }

        public MediasendMailing GetMailing(int mailingId)
        {
            try
            {
                var dict = MakeBaseDictionary();
                dict.Add("mailing_id", mailingId.ToString());
                var record = CakeMailing.GetInfo(dict);
                var campaign = GetCampaign(int.Parse(record["campaign_id"].ToString()));
                //var list = GetList(int.Parse(record["list_id"].ToString()));
                var mailing = MediasendMailing.MakeMailing(this, campaign, int.Parse(record["list_id"].ToString()),
                                                           record);
                mailing.SenderName = record["sender_name"].ToString();
                mailing.SenderEmail = record["sender_email"].ToString();
                return mailing;
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return null;
            }
        }

        public List<MediasendMailing> GetMailings(MediasendCampaign campaign)
        {
            try
            {
                var dict = MakeBaseDictionary();
                dict.Add("campaign_id", campaign.ID.ToString());
                var records = CakeMailing.GetList(dict);
                var mailings = new List<MediasendMailing>();
                foreach (var kvp in records)
                {
                    var data = kvp.Value as Dictionary<string, object>;
                    var list = GetList(int.Parse(data["list_id"].ToString()));
                    mailings.Add(MediasendMailing.MakeMailing(this, campaign, list.ID, data));
                }
                return mailings;
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return null;
            }
        }

        public MediasendMailing CreateMailing(MediasendCampaign campaign, string name)
        {
            return CreateMailing(campaign.ID, name);
        }

        public MediasendMailing CreateMailing(int campaignId, string name)
        {
            try
            {
                var dict = MakeBaseDictionary();
                dict.Add("campaign_id", campaignId.ToString());
                dict.Add("name", name);

                var record = CakeMailing.Create(dict);
                return GetMailing(record);
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return null;
            }
        }

        public bool DeleteMailing(MediasendMailing mailing)
        {
            try
            {
                var dict = MakeBaseDictionary();
                dict.Add("mailing_id", mailing.ID.ToString());
                return CakeMailing.Delete(dict);
            }
            catch (CakeException ex)
            {
                LastError = ex.Message;
                return false;
            }
        }
    }
}